package com.ujiuye.web;

import com.ujiuye.entity.Student;
import com.ujiuye.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

//@WebServlet("/queryAll")
//public class QueryStuServlet extends HttpServlet {
//    @Override
//    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //解决中文乱码
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
//        PrintWriter pw = resp.getWriter();
//        //调用Student的service层的查询学生方法
//        StudentService ss = new StudentService();
//        List<Student> list = ss.queryStudents();
//        pw.println("<table border='1px' align='center' width='60%' cellspacing='0'>");
//        pw.println("<tr><th>学号</th><th>姓名</th><th>年龄</th><th>电话</th><th>生日</th><th>性别</th><th>操作</th></tr>");
//        //遍历list集合
//        for(Student s:list){
//            pw.println("<tr>" +
//                       "<td>"+s.getSid()+"</td>" +
//                       "<td>"+s.getSname()+"</td>" +
//                       "<td>"+s.getAge()+"</td>" +
//                       "<td>"+s.getTel()+"</td>" +
//                       "<td>"+s.getSbir()+"</td>" +
//                       "<td>"+s.getsex()+"</td>" +
//                       "<td><a href=''>删除</a><td>"+
//                       "</tr>");
//        }
//        pw.println("</table>");
//        pw.println("<div style='height:30px;width:75%;margin:20px auto;position:absolute'><a href='addStu.html' style='position:absolute;right:0px'>添加学生</a><div>");
//    }
//}

@WebServlet("/queryAll")
public class QueryStuServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决中文乱码
//        req.setCharacterEncoding("utf-8");
//        resp.setContentType("text/html;charset=utf-8");
        PrintWriter pw = resp.getWriter();
        //调用Student的service层的查询学生方法
        StudentService ss = new StudentService();
        List<Student> list = ss.queryStudents();
        //数据绑定
        req.setAttribute("list",list); //${list}
        //req.setAttribute("stu",new Student(xxxxx));   //${stu.sname}
        //转发-->让queryStu.jsp显示数据
        req.getRequestDispatcher("queryStu.jsp").forward(req,resp);
    }
}